<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <el-form
              ref="userForm"
              :model="userInfo"
              :rules="rules"
              label-width="120px"
              style="margin-left: 120px; margin-top:30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="修改密码" prop="password2">
                <el-input v-model="userInfo.password2" placeholder="请输入新密码" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <component :is="userInfoComponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <component :is="jobInfoComponent" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>

</template>

<script>
import { getUserDetailById } from '@/api/user'
// import { saveUserDetailById } from '@/api/employees'
import { saveUserDetailById } from '@/api/user'
import userInfo from '@/views/employees/components/user-info'
import jobInfo from '@/views/employees/components/job-info'
// 15：00之前

export default {
  components: {
    userInfoComponent: userInfo,
    jobInfoComponent: jobInfo
  },
  data() {
    return {
      userInfoComponent: 'userInfoComponent',
      jobInfoComponent: 'jobInfoComponent',
      userInfo: {},
      rules: {
        username: [
          {
            required: true,
            message: '用户名不能为空',
            trigger: 'blur'
          }
        ],
        password2: [
          {
            required: true,
            trigger: 'blur',
            message: '密码不能为空'
          },
          {
            min: 6,
            max: 9,
            trigger: 'blur',
            message: '密码必须6-9位'
          }
        ]
      },
      userId: this.$route.params.id
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    async getUserDetailById() {
      // 我们调用了获取员工个人信息的接口，同时把返回的数据存储到了userInfo里面
      this.userInfo = await getUserDetailById(this.userId)
    },
    async saveUser() {
      try {
        //  先进性表单校验
        await this.$refs.userForm.validate()
        // 调用接口，提交数据
        await saveUserDetailById({
          ...this.userInfo,
          password: this.userInfo.password2
        })
        // 成功的提示
        this.$message.success('保存员工信息成功')
      } catch (e) {
        // 捕获错误的目的，是为了防止控制台报错
        console.log(e)
      }
    }
  }
}

// 所有组件的created钩子里面做一件相同的事情(alert(234))
</script>
